<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class koudai_adminControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$where=" siteid=".SITEID;
			$url=APPADMIN."?m=koudai_admin&a=default";
			$limit=20;
			$start=get("per_page","i");
			$shop=M("koudai")->selectRow(array("where"=>" siteid=".SITEID));
			 
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" adminid DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=M("koudai_admin")->select($option,$rscount);
			if($data){
				foreach($data as $v){
					$shopids[]=$v['shopid'];					
				}
				$shops=M('koudai')->getShopByIds($shopids);
				foreach($data as $k=>$v){
					$v['shopname']=$shops[$v['shopid']]['title'];
					$data[$k]=$v;
				} 
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"shop"=>$shop
				)
			);
			$this->smarty->display("koudai_admin/index.html");
		}
		
		public function onAdd(){
			$adminid=get_post("adminid","i");
			 
			if($adminid){
				$data=M("koudai_admin")->selectRow(array("where"=>"adminid={$adminid}"));
				$shopid=$data['shopid'];
			}
			
			$shop=M("koudai")->selectRow(array("where"=>" siteid=".SITEID));
			$this->smarty->assign(array(
				"data"=>$data,
				"shop"=>$shop
			));
			$this->smarty->display("koudai_admin/add.html");
		}
		
		public function onSave(){
			
			$adminid=get_post("adminid","i");
			$data=M("koudai_admin")->postData();
			$shop=M("koudai")->selectRow(array("where"=>" siteid=".SITEID));
			
			$data['adminname']=$adminname=post('adminname','h');
			
			$row=M("koudai_admin")->selectRow("adminname='".$adminname."'");
		
			if($row && $row['adminid']!=$adminid){
				$this->goall("账号已经存在",1);
			}
			if($adminid){
				M("koudai_admin")->update($data,"adminid='$adminid'");
			}else{
				if(post('password')){
					if(post('password')!=post('password2')) $this->goall("两次密码输入不一致",1);
					$data["salt"]=rand(1000,9999);
					$data["password"]=umd5(post("password","h").$data["salt"]);
					
				}
				$data['shopid']=$shop['id'];
				$data['siteid']=SITEID;
				M("koudai_admin")->insert($data);
			}
			
			$this->goall("保存成功");
		}
		
		public function onPassword(){
			$adminid=get_post("adminid","i");
			$data=M("koudai_admin")->selectRow(array("where"=>"adminid={$adminid}"));
			$shop=M("koudai")->selectRow(array("where"=>" siteid=".SITEID));
			$this->smarty->assign(array(
				"data"=>$data,
				"shop"=>$shop
			));
			$this->smarty->display("koudai_admin/password.html");
		}
		
		public function onPasswordSave(){
			if(post('password')!=post('password2')) $this->goall("两次密码输入不一致",1);
			$adminid=get_post("adminid","i");
			$row=M("koudai_admin")->selectRow("adminid='".$adminid."'");
			if($row['siteid']!=SITEID){
				$this->goAll("数据出错");
			}
			$data["salt"]=rand(1000,9999);
			$data["password"]=umd5(post("password","h").$data["salt"]);
			M("koudai_admin")->update($data,"adminid='$adminid'");
			$this->goall("保存成功");
		}
		
		
	}

?>